package com.sulink.numberkeyboard.popWindow;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.sulink.numberkeyboard.R;
import com.sulink.numberkeyboard.widget.Keyboard;
import com.sulink.numberkeyboard.widget.PayEditText;

public abstract class BasePayPop extends PopupWindow {


    protected Keyboard mKeyboard;

    protected PayEditText mPayEditText;

    private PayEnterPassWordListener mListener;

    protected Context mContext;


    public BasePayPop(Context context) {
        super(context);
        mContext = context;
        View view = View.inflate(context, getLayoutResource(), null);
        setContentView(view);
        initView(view);
        initPop();
    }


    private void initPop() {
        setBackgroundDrawable(null);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
    }

    public void setOnPayListener(PayEnterPassWordListener listener) {
        mListener = listener;
    }

    protected abstract int getLayoutResource();

    protected void initView(View view) {

        mKeyboard = view.findViewById(R.id.keyboardView);
        mPayEditText = view.findViewById(R.id.payEditText);

        mKeyboard.setOnClickKeyboardListener(new Keyboard.OnClickKeyboardListener() {
            @Override
            public void onKeyClick(int position, String value) {
                if (position == 11) {
                    mPayEditText.remove();
                } else if (position == 9) {

                    dismiss();

                    if (mListener != null) {
                        mListener.onCancel();
                    }
                } else {
                    mPayEditText.add(value);
                }
            }
        });

        /**
         * 当密码输入完成时的回调
         */
        mPayEditText.setOnInputFinishedListener(new PayEditText.OnInputFinishedListener() {
            @Override
            public void onInputFinished(String password) {

                if (mListener != null) {
                    mListener.onInputFinished(password);
                }
            }
        });
    }


    public interface PayEnterPassWordListener {

        void onCancel();

        void onInputFinished(String passWord);

    }

}
